package com.bw.skill.mvvm.view;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.view.BaseActivity;
import com.bw.mvvm_common.network.load.LoadImage;
import com.bw.skill.BR;
import com.bw.skill.R;
import com.bw.skill.TimeService;
import com.bw.skill.action.SkillAction;
import com.bw.skill.databinding.ActivitySkillDetailBinding;
import com.bw.skill.mvvm.viewmodel.SecDetailViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;

public class SkillDetailActivity extends BaseActivity<ActivitySkillDetailBinding, SecDetailViewModel> {
    private TimeService timeService;
    boolean serviceFlag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        Bundle bundleExtra = getIntent().getBundleExtra(UIField.PAGEBUNDLE);
        String goodsDesc = bundleExtra.getString("goodsDesc");
        String goodsIcon = bundleExtra.getString("goodsIcon");
        String goodsSku = bundleExtra.getString("goodsSku");
        int goodsCount = bundleExtra.getInt("goodsCount");
        int goodsId = bundleExtra.getInt("goodsId");
        String price = bundleExtra.getString("goodsPrice");
        int userId = bundleExtra.getInt("userId");
        long time = bundleExtra.getLong("time");
        vm.callbundle(goodsDesc,goodsIcon,goodsSku,goodsCount,goodsId,price,userId,time);
    }

    @Override
    public int bindLayout() {

        return R.layout.activity_skill_detail;
    }

    @Override
    public int initVerable() {
        return BR.vm;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(serviceFlag){
            Intent intent = new Intent(this, TimeService.class);
            bindService(intent, connection, BIND_AUTO_CREATE);
        }


    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(connection);
    }

    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            TimeService.TimeBinder binder = (TimeService.TimeBinder) service;
            timeService = binder.getTimeService();

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };



    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        Intent intent = new Intent(this, TimeService.class);
        stopService(intent);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void get(SkillAction action){
        if(action.action.equals("ss")){
            Intent intent = new Intent(this, TimeService.class);
            intent.putExtra("time",action.nowTime);
            startService(intent);
            Intent bindIntent = new Intent(this, TimeService.class);
            bindService(bindIntent, connection, BIND_AUTO_CREATE);
        }else if (action.action.equals("SecDeatilTime")){
            long nowTime = timeService.getTime();
            LogUtils.e("bb",nowTime);
            vm.requestTime(nowTime);
        }
    }




}